package com.wsj.a.cropdemo;

import android.Manifest;
import android.content.Intent;
import android.databinding.DataBindingUtil;
import android.support.annotation.NonNull;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Toast;

import com.wsj.a.cropdemo.databinding.ActivityMainBinding;

import java.util.List;

import pub.devrel.easypermissions.AfterPermissionGranted;
import pub.devrel.easypermissions.AppSettingsDialog;
import pub.devrel.easypermissions.EasyPermissions;

public class MainActivity extends AppCompatActivity implements EasyPermissions.PermissionCallbacks{

    ActivityMainBinding mBinding;
    //需要用到的权限
    private String[] perms = {Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.READ_EXTERNAL_STORAGE};
    private final static int REQUEST_CODE=1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mBinding= DataBindingUtil.setContentView(this,R.layout.activity_main);
        mBinding.setOnClick(this);
        initWebView();
    }

    public void initWebView(){
        mBinding.webview.getSettings().setJavaScriptEnabled(true);
        mBinding.webview.setDrawingCacheEnabled(true);//
        mBinding.webview.setWebViewClient(new WebViewClient(){
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                view.loadUrl(url);
                return super.shouldOverrideUrlLoading(view, url);
            }
        });
        mBinding.webview.loadUrl("https://weibo.com/1192329374/GdTzWbapH?ref=feedsdk&type=comment#_rnd1524811240946");
    }

    /***webview的长截图**/
    public void onCropWebView(View view){
         requestPermissions();
    }

    /**
     * 一些权限已授权
     * @param requestCode
     * @param perms
     */
    @Override
    public void onPermissionsGranted(int requestCode, @NonNull List<String> perms) {

    }

    /***
     * 一些权限被拒绝
     * @param requestCode
     * @param perms
     */
    @Override
    public void onPermissionsDenied(int requestCode, @NonNull List<String> perms) {
        //申请权限
        new AppSettingsDialog.Builder(this)
                .setTitle("尊敬的用户")
                .setRationale("应用截图需要读写内存权限")
                .setRequestCode(REQUEST_CODE)
                .build()
                .show();
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == REQUEST_CODE) {
            webViewCrop();
        }
    }

    /***
     * 请求权限
     * @param requestCode
     * @param permissions
     * @param grantResults
     */
    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        EasyPermissions.onRequestPermissionsResult(requestCode, permissions, grantResults, this);
    }

    @AfterPermissionGranted(REQUEST_CODE)
    public void onPermissionOK(){
        webViewCrop();
    }

    /**
     * 请求权限
     */
    private void requestPermissions() {
        if (EasyPermissions.hasPermissions(this, perms)) {
            //获取权限成功
            webViewCrop();
        } else {
            //获取选取失败
            EasyPermissions.requestPermissions(this,"应用截图需要读写内存权限" , REQUEST_CODE, perms);
        }
    }

    /**webview页面截图**/
    public void webViewCrop(){
        ContentToPictureUtils.webviewContent2Png(this, mBinding.webview);
        Toast.makeText(this, "已保存到相册中", Toast.LENGTH_SHORT).show();
    }

}
